package designPattern.singleton;

public class SingletonTest6 {
    public static void main(String[] args) {
        Singleton6 instance1 = Singleton6.getInstance();
        Singleton6 instance2 = Singleton6.getInstance();

        //判断两个对象是否相同
        System.out.println(instance2==instance1);

        System.out.println("instance1.hashCode() = " + instance1.hashCode());
        System.out.println("instance2.hashCode() = " + instance2.hashCode());
    }
}

//饿汉式（双重检查）

class Singleton6 {

    private static Singleton6 instance;

    //私有化构造器
    private Singleton6() {
    }

    //静态内部类，该类中有一个静态属性Singleton
    //利用了静态内部类不会被立即加载的机制，等到被调用时才会被加载
    private static class SingletonInstance {
        private static final Singleton6 INSTANCE = new Singleton6();
    }
    //公共静态方法，双重检查，解决线程安全问题与懒加载问题，性能优秀
    public static Singleton6 getInstance() {
        return SingletonInstance.INSTANCE;
    }
}